package qs.openxt.libs.util;

import java.util.List;

import android.app.NotificationManager;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
* 本地存储消息格式定义
* [
	{
	  uuid: "唯一标识�?", //默认生成方式UUID
	  title: "标题", //内容可以自定�? 具体根据推�?�消息来做处�?
	  message: "消息内容", //内容可以自定�? 具体根据推�?�消息来做处�?
	  custom_content: "自定义内�?", //内容可以自定�? 具体根据推�?�消息来做处�?
	  state: "" //消息状�??  read/unread
	},
	...
  ]
* @author 赵颖�? 联系方式 13805848274
*/
public class MessageUtils {
	public static final String PREFS_NAME = "MSGS";
	public static final String TYPE_ALL = "all";
	public static final String TYPE_READ= "read";
	public static final String TYPE_UNREAD = "unread";
	
	/**
	 * 获取本地消息
	 * @param mContent
	 * @return
	 */
	public static JSONArray getMessages(Context mContent, String state){
		if(StringUtil.isNull(state))
			state = TYPE_ALL;
		String tags = Utils.getLocalStorage(mContent, Utils.TAGS);
		if(StringUtil.isNull(tags))
			return null;
		
		String append = Utils.getLocalStorage(mContent, PREFS_NAME);
		if(StringUtil.isNull(append))
			return null;
		try {
			JSONObject $val = JSON.parseObject(append);
			if(TYPE_ALL.equals(state))
				return $val.getJSONArray(tags);
			JSONArray array = $val.getJSONArray(tags);
			JSONArray msgs = new JSONArray();
			for (int i = 0, j = array.size(); i < j; i++) {
				JSONObject item = array.getJSONObject(i);
				if(item == null)
					continue;
				if(state.equals(item.getString("state"))){
					msgs.add(item);
				}
			}
			return msgs;
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 添加本地消息
	 * @param mContent
	 * @param msg
	 */
	public static void addMessage(Context mContent, JSONObject msg){
		try {
			String tags = Utils.getLocalStorage(mContent, Utils.TAGS);
			if(StringUtil.isNull(tags))
				return;
			
			JSONObject $val = null;
			JSONArray msgs = null;
			String append = Utils.getLocalStorage(mContent, PREFS_NAME);
			if(StringUtil.isNull(append)){
				$val = new JSONObject();
				msgs = new JSONArray();
				msgs.add(msg);
				
				$val.put(tags, msgs);
			}else {
				$val = JSON.parseObject(append);
				
				msgs = $val.getJSONArray(tags);
				if(msgs == null){
					msgs = new JSONArray();
					$val.put(tags, msgs);
				}
				
				msgs.add(msg);
			}
			Utils.setLocalStorage(mContent, PREFS_NAME, $val.toString());
		} catch (Exception e) {
		}
	}
	
	/**
	 * 设置本地消息状�??
	 * @param mContent
	 * @param ids
	 */
	public static void setMessage(Context mContent, List<String> ids, String state){
		String tags = Utils.getLocalStorage(mContent, Utils.TAGS);
		if(StringUtil.isNull(tags))
			return;
		
		String append = Utils.getLocalStorage(mContent, PREFS_NAME);
		if(StringUtil.isNull(append))
			return;
		try {
			JSONObject $val = JSON.parseObject(append);
			
			JSONArray msgs = $val.getJSONArray(tags);
			for (int i = 0, j = msgs.size(); i < j; i++) {
				JSONObject item = msgs.getJSONObject(i);
				if(item == null)
					continue;
				if(ids.contains(item.getString("uuid"))){
					item.put("state", state);
				}
			}
			
			Utils.setLocalStorage(mContent, PREFS_NAME, $val.toString());
			
			if("read".equals(state)){
				NotificationManager notificationManager = (NotificationManager) mContent.getSystemService(Context.NOTIFICATION_SERVICE);
				for (int i = 0, j = ids.size(); i < j; i++) {
					notificationManager.cancel(Integer.valueOf(ids.get(i)));
				}
			}
		} catch (Exception e) {
		}
	}
	
	/**
	 * 删除本地消息
	 * @param mContent
	 * @param ids null全部清空
	 */
	public static void delMessage(Context mContent, List<String> ids){
		String tags = Utils.getLocalStorage(mContent, Utils.TAGS);
		if(StringUtil.isNull(tags))
			return;
		
		String append = Utils.getLocalStorage(mContent, PREFS_NAME);
		if(StringUtil.isNull(append))
			return;
		if(ids == null){
			Utils.removeLocalStorage(mContent, PREFS_NAME);
			try {
				NotificationManager notificationManager = (NotificationManager) mContent.getSystemService(Context.NOTIFICATION_SERVICE);
				notificationManager.cancelAll();
			} catch (Exception e) {
			}
			return;
		}
		try {
			JSONObject $val = JSON.parseObject(append);
			
			JSONArray msgs = $val.getJSONArray(tags);
			JSONArray new_msgs = new JSONArray();
			for (int i = 0, j = msgs.size(); i < j; i++) {
				JSONObject item = msgs.getJSONObject(i);
				if(item == null)
					continue;
				if(ids.contains(item.getString("uuid"))){
					continue;
				}
				new_msgs.add(item);
			}
			
			Utils.setLocalStorage(mContent, PREFS_NAME, new_msgs.toString());
			
			NotificationManager notificationManager = (NotificationManager) mContent.getSystemService(Context.NOTIFICATION_SERVICE);
			for (int i = 0, j = ids.size(); i < j; i++) {
				notificationManager.cancel(Integer.valueOf(ids.get(i)));
			}
		} catch (Exception e) {
		}
	}
}
